﻿using Microsoft.Win32;

namespace Font2Outline
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            List<DisplayValue> list = new List<DisplayValue>();
            string fontPath = "C:/Windows/Fonts/";
            RegistryKey? key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts");
            if (key == null)
                return;
            List<string> names = key.GetValueNames().OrderBy(it => it).ToList();
            foreach (string str in names)
            {

                string? path = key.GetValue(str)?.ToString();
                string name = str;
                if (name.EndsWith("(TrueType)"))
                {
                    name = name.Replace("(TrueType)", "").Trim();
                }
                path = path?.Replace("\\", "/");
                path ??= string.Empty;
                if (!path.Contains("/"))
                    path = fontPath + path;
                list.Add(new DisplayValue { DisplayName = name, Value = path });
            }
            cbFont.DataSource = list;
            cbFont.DisplayMember = "DisplayName";
            cbFont.ValueMember = "Value";
        }

        private unsafe void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtChar.Text.Trim()))
                {
                    MessageBox.Show("请输入字符");
                    return;
                }
                string str = txtChar.Text.Trim();
                string path = (string)cbFont.SelectedValue;
                pictureBox.Image = FontUtils.GetOutlineImage(str, path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnGenerateSVG_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtChar.Text.Trim()))
                {
                    MessageBox.Show("请输入字符");
                    return;
                }
                string str = txtChar.Text.Trim();
                string path = (string)cbFont.SelectedValue;
                pictureBox.Image = FontUtils.GetOutlineImage(str, path);
                string svg = FontUtils.GetOutlineSVG(str, path);
                //选择保存路径
                SaveFileDialog sfd = new SaveFileDialog();
                //设置文件类型 
                sfd.Filter = "SVG（*.svg）|*.svg";
                //设置默认文件类型显示顺序 
                sfd.FilterIndex = 1;
                //保存对话框是否记忆上次打开的目录 
                sfd.RestoreDirectory = true;
                //点了保存按钮进入 
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    string fileName = sfd.FileName.ToString(); //获得文件路径  
                    File.WriteAllText(fileName, svg);
                    MessageBox.Show("保存成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    public class DisplayValue
    {
        public string? DisplayName { get; set; }
        public string? Value { get; set; }
    }
}
